package com.newtouch.cq.security.config.handle;

import cn.hutool.json.JSONUtil;
import com.newtouch.cq.lang.em.BaseExceptionEnum;
import com.newtouch.cq.lang.result.MccObjectResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(200);
        response.getWriter().write(JSONUtil.toJsonStr(MccObjectResult.fail(BaseExceptionEnum.INSUFFICIENT_PERMISSIONS)));
    }
}
